//
//  ScenicDetailStoryTableViewCell.swift
//  LeTu
//
//  Created by g1 on 2024/1/12.
//

import UIKit
import Lottie

class ScenicDetailStoryTableViewCell: BaseTableViewCell {

    var clickPlayBtnHandle: ((Int, ScenicStoryModel) -> Void)?
    var clickLockBtnHandle: ((Int, ScenicStoryModel) -> Void)?
    var clickTryOutBtnHandle: ((Int, ScenicStoryModel) -> Void)?
    
    private var storyModel = ScenicStoryModel()
    private var index = 0
    private var isPlaying = false
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        configUI()
    }
    static func getCellHeight() -> CGFloat {
        return 80
    }
    func setListenPermission(_ listenPermission: Bool) {
        
        playingView.isHidden = !isPlaying
        if isPlaying {
            lockBtn.isHidden = !playingView.isHidden
            tryOutBtn.isHidden = !playingView.isHidden

        } else {
            
            lockBtn.isHidden = listenPermission
            
            if listenPermission == false {
                tryOutBtn.isHidden = storyModel.payStatus == 0 ? false:true
                lockBtn.isHidden = !tryOutBtn.isHidden
            } else {
                tryOutBtn.isHidden = true
            }
        }

    }
    func setStoryModel(_ storyModel: ScenicStoryModel, index: Int, isPlaying: Bool) {
        self.storyModel = storyModel
        self.index = index
        self.isPlaying = isPlaying
        countLabel.text = "\(index+1)"
        nameLabel.text = LanguageManager.shared.currentLanguage == .zh ? storyModel.chineseAudioName:storyModel.englishAudioName
        
        updatePlaying(isPlaying: isPlaying)
        
        tryOutBtn.setTitle("试听".localized)
        tryOutBtn.setTitleEdgeInsets(UIEdgeInsets(top: 0, left: LanguageManager.shared.currentLanguage == .zh ? 12:0, bottom: 0, right: LanguageManager.shared.currentLanguage == .zh ? 4:0))
    }
    func updatePlaying(isPlaying: Bool) {
        playBtn.setImage(UIImage(named: isPlaying ? "story_pause_icon":"story_play_icon"))
        
        if isPlaying {
            playingView.play()
        } else {
            playingView.stop()
        }
    }
    
    // MARK: - UIButtonAction
    @objc func playBtnAction() {
        guard lockBtn.isHidden == true else {
            lockBtnAction()
            return
        }
        guard let handle = clickPlayBtnHandle else {
            send(event: ScenicDetailVC.TransferEvent.playStory(index))
            return
        }
        handle(index, storyModel)
    }
    @objc func lockBtnAction() {
        guard let handle = clickLockBtnHandle else {
            send(event: ScenicDetailVC.TransferEvent.lockStory(index))
            return
        }
        handle(index, storyModel)
    }
    @objc func tryOutBtnAction() {
        guard let handle = clickTryOutBtnHandle else {
            send(event: ScenicDetailVC.TransferEvent.tryOutStory(index))
            return
        }
        handle(index, storyModel)
    }
    
    // MARK: - configUI
    func configUI() {
        
        contentView.addSubview(backView)
        backView.addSubview(countLabel)
        backView.addSubview(nameLabel)
        backView.addSubview(playBtn)
        backView.addSubview(lockBtn)
        backView.addSubview(tryOutBtn)
        backView.addSubview(playingView)
        
        backView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.top.bottom.right.equalToSuperview()
        }
        countLabel.snp.makeConstraints { make in
            make.left.centerY.equalToSuperview()
        }
        playBtn.snp.makeConstraints { make in
            make.right.equalToSuperview()
            make.width.equalTo(10+46)
            make.top.bottom.equalToSuperview()
        }
        lockBtn.snp.makeConstraints { make in
            make.right.equalTo(playBtn.snp.left)
            make.width.equalTo(20+10+10)
            make.top.bottom.equalToSuperview()
        }
        tryOutBtn.snp.makeConstraints { make in
            make.right.equalTo(playBtn.snp.left)
            make.width.equalTo(12+30+4)
            make.top.bottom.equalToSuperview()
        }
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(countLabel.snp.right).offset(20)
            make.centerY.equalToSuperview()
            make.right.lessThanOrEqualTo(lockBtn.snp.left).offset(-10)
        }
        playingView.snp.makeConstraints { make in
            make.center.equalTo(tryOutBtn)
            make.width.height.equalTo(12)
        }
        
        playBtn.addTarget(self, action: #selector(playBtnAction))
        lockBtn.addTarget(self, action: #selector(lockBtnAction))
        tryOutBtn.addTarget(self, action: #selector(tryOutBtnAction))
        
    }
    private lazy var backView: UIView = {
        let backView = UIView()
        return backView
    }()
    private lazy var countLabel: UILabel = {
        let countLabel = UILabel()
        countLabel.font = kSetPingFangMedium(16)
        countLabel.textColor = UIColor.hex(hexString: "4F5D61")
        return countLabel
    }()
    private lazy var nameLabel: UILabel = {
        let nameLabel = UILabel()
        nameLabel.font = kSetPingFangRegular(16)
        nameLabel.textColor = UIColor.hex(hexString: "4F5D61")
        return nameLabel
    }()
    private lazy var playBtn: CustomClickableView = {
        let playBtn = CustomClickableView()
        playBtn.setImage(UIImage(named: "story_play_icon"))
        let cellHieght = ScenicDetailStoryTableViewCell.getCellHeight()
        playBtn.setImageEdgeInsets(UIEdgeInsets(top: (cellHieght - 20)/2, left: 10, bottom: (cellHieght - 20)/2, right: 26))
        return playBtn
    }()
    private lazy var lockBtn: CustomClickableView = {
        let lockBtn = CustomClickableView()
        lockBtn.setImage(UIImage(named: "story_lock_icon"))
        let cellHieght = ScenicDetailStoryTableViewCell.getCellHeight()
        lockBtn.setImageEdgeInsets(UIEdgeInsets(top: (cellHieght - 12)/2, left: 20, bottom: (cellHieght - 12)/2, right: 10))
        return lockBtn
    }()
    private lazy var tryOutBtn: CustomClickableView = {
        let tryOutBtn = CustomClickableView()
        tryOutBtn.setFont(kSetPingFangRegular(12))
        tryOutBtn.setTitleColor(UIColor.hex(hexString: "C9A483"))
        tryOutBtn.isHidden = true
        return tryOutBtn
    }()
    let playingView: LottieAnimationView = {
        let playingView = LottieAnimationView(name: "playing")
        playingView.loopMode = .loop
        return playingView
    }()
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
